﻿Imports System.IO
Imports AFS.Aspnet.Collaboration.Site.FallBackService
Imports AFS.Aspnet.Collaboration.Site.MandateAufstellung

Module MandetLogic

    Public addpagewasloaded As Boolean = False
    Public listofmandate As New List(Of MandateAufstellung.MandateAnzahlen)
    Public mandateDb As MandateAufstellung.Main
    Public db As AFS.Aspnet.Collaboration.Site.ArbAuf.Main
    Public gpsdb As GPSBilderDatenbank.Main


#Region "public proceduren"
    Public Function DbPath() As String
        Dim retval = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ArbeitsaufzeichnungenV1.s3db"))
        If System.IO.File.Exists(retval) = False Then
            Dim rtval = DownloadDatabaseFileFromBackFallServer()
            If rtval = True Then
                Return retval
            Else
                Return ""
            End If
        Else
            Return retval
        End If
    End Function

    Public Function GPSBilderPath() As String
        Dim retval = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, GPSBilderDBName)) ' "GPSBilderUpload.s3db"))
        If System.IO.File.Exists(retval) = False Then
            Dim rtval = DownloadGPSBilderFileFromBackFallServer()
            If rtval = True Then
                Return retval
            Else
                Return ""
            End If
        Else
            Return retval
        End If
    End Function

    Public Function MandateDBPath() As String
        Dim Retval = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Mandate.s3db"))
        If System.IO.File.Exists(Retval) = False Then
            Dim rtval = DownloadMandateFileFromBackFallServer()
            If rtval = True Then
                Return Retval
            Else
                Return ""
            End If
        Else
            Return Retval
        End If
    End Function

    Public Function VariousDBPath(ByVal name As String)
        Return Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, name))
    End Function

    Public Function MyFile() As String
        'Return System.IO.Path.Combine("../ClientBin", "users.txt")
        '        Return Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "mandate.xml"))
        Return MyFile("mandate.xml")
    End Function

    Public Function MyFile(ByVal Filename As String) As String
        'Return System.IO.Path.Combine("../ClientBin", "users.txt")
        Return Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Filename))
    End Function

#End Region

#Region "Logic for mandate"
    Function TransfromToMandateNormalType(ByVal a As List(Of MandateAufstellung.MandateAnzahlen)) As List(Of Mandat)
        Dim retval As New List(Of Mandat)

        For Each b In a
            Dim ntype As New Mandat
            With ntype
                .Anzeigen = b.Anzeigen
                .Datum = b.Datum
                .Stornos = b.Storno
                .PgAnzahl = b.Pg
                .StvoAnzahl = b.StVo
                .TB02 = b.TB02
                .Rayon = b.Rayon
                .Type = b.Typ
            End With
            retval.Add(ntype)
        Next
        Return retval
    End Function


    Public Sub InitMandateDBFile()

        If System.IO.File.Exists(MandateDBPath) Then
            Dim mand As New SQLite.SQLiteConnectionStringBuilder
            mand.ConnectionString = "DbLinqProvider=Sqlite ; data source=" & MandateDBPath()

            Dim mandcon As New SQLite.SQLiteConnection(mand.ToString)
            mandateDb = New MandateAufstellung.Main(mandcon)
        Else
            If DownloadMandateFileFromBackFallServer() Then
                InitMandateDBFile()
            Else
                My.Computer.FileSystem.WriteAllText(files, "Download Mandate from server failed " & Date.Now.ToString & ";" & vbCrLf, True)
            End If
        End If

    End Sub

    Public Sub InitBilderDBFile()

        If System.IO.File.Exists(GPSBilderPath) Then
            Dim mand As New SQLite.SQLiteConnectionStringBuilder
            mand.ConnectionString = "DbLinqProvider=Sqlite ; data source=" & GPSBilderPath()

            Dim mandcon As New SQLite.SQLiteConnection(mand.ToString)
            gpsdb = New GPSBilderDatenbank.Main(mandcon)
        Else
            If DownloadGPSBilderFileFromBackFallServer() Then
                InitBilderDBFile()
            Else
                My.Computer.FileSystem.WriteAllText(files, "Download Mandate from server failed " & Date.Now.ToString & ";" & vbCrLf, True)
            End If
        End If
    End Sub

    Public Sub InitArbeitsaufzeichnungenDBFile()

        If System.IO.File.Exists(DbPath) Then
            Dim mand As New SQLite.SQLiteConnectionStringBuilder
            mand.ConnectionString = "DbLinqProvider=Sqlite ; data source=" & DbPath()

            Dim mandcon As New SQLite.SQLiteConnection(mand.ToString)
            db = New ArbAuf.Main(mandcon)
        Else
            If DownloadMandateFileFromBackFallServer() Then
                InitArbeitsaufzeichnungenDBFile()
            Else
                My.Computer.FileSystem.WriteAllText(files, "Download ArbAuf from server failed " & Date.Now.ToString & ";" & vbCrLf, True)
            End If
        End If
    End Sub

    Public Function InitVariousDbFileConnString(ByVal name As String) As SQLite.SQLiteConnection
        Dim mand As New SQLite.SQLiteConnectionStringBuilder
        mand.ConnectionString = "DbLinqProvider=Sqlite ; data source=" & VariousDBPath(name)

        Dim mandcon As New SQLite.SQLiteConnection(mand.ToString)
        Return mandcon
    End Function

    Function ReadAllUsersFromXMLFile() As List(Of MandateAnzahlen)
        If mandateDb Is Nothing Then
            InitMandateDBFile()
        End If

        Dim a = (From b In mandateDb.MandateAnzahlen
                Select b).ToList

        Return a
    End Function







    Function SumUpAllMandate() As Integer
        Dim summen As Integer = 0
        For Each a In listofmandate
            summen += CInt(a.Pg)
            summen += CInt(a.StVo)
        Next
        Return summen
    End Function

    Function SumUpPG() As Integer
        Dim summen As Integer = 0
        For Each a In listofmandate
            summen += CInt(a.Pg)
        Next
        Return summen
    End Function

    Function SumUpStVo() As Integer
        Dim summen As Integer = 0
        For Each a In listofmandate
            summen += CInt(a.StVo)
        Next
        Return summen
    End Function

#Region "BackFallServiceRoutines"
    Public BackFallService As New FallBackService.BackYardServiceClient
    ' Dim BackFallService As New TempLocalTestService.BackYardServiceClient


    Function DownloadMandateFileFromBackFallServer() As Boolean
        Try
            Dim downrequest As New DownloadRequest
            Dim filerequest As New RemoteFileInfo

            downrequest.FileName = "mandate.s3db"

            BackFallService.DownloadFile(downrequest.FileName, False, filerequest.FileByteStream)

            Dim buffer As Byte() = New Byte(6499) {}
            Dim bytesRead As Integer = 0

            Dim writestream As New System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "mandate.s3db"), IO.FileMode.Create)

            bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            While bytesRead > 0
                writestream.Write(buffer, 0, bytesRead)
                writestream.Flush()
                buffer = New Byte(6499) {}
                bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            End While

            writestream.Close()

            Dim HasErrors As Boolean = False
            Dim OpType As String = ""
            Dim ErrorDescr As Exception = BackFallService.GetOperationStatus(HasErrors, OpType)
            If HasErrors = True Then
                Debug.WriteLine(ErrorDescr.Message.ToString)
                Return False
            Else
                Return True
            End If
            Return True
        Catch ex As Exception
            My.Computer.FileSystem.WriteAllText(files, "DownloadMandate failed: Error: " & ex.Message.ToString & vbCrLf & ex.Source.ToString & Date.Now.ToString & ex.Message.ToString & vbCrLf & ex.Source.ToString & ";" & vbCrLf, True)
            Return False
        End Try
    End Function

    Function DownloadMandateFileFromBackFallServerForCombine() As Boolean
        Try
            Dim downrequest As New DownloadRequest
            Dim filerequest As New RemoteFileInfo

            downrequest.FileName = "mandate.s3db"

            BackFallService.DownloadFile(downrequest.FileName, False, filerequest.FileByteStream)

            Dim buffer As Byte() = New Byte(6499) {}
            Dim bytesRead As Integer = 0

            Dim writestream As New System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tempmandate.s3db"), IO.FileMode.Create)

            bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            While bytesRead > 0
                writestream.Write(buffer, 0, bytesRead)
                writestream.Flush()
                buffer = New Byte(6499) {}
                bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            End While

            writestream.Close()

            Dim HasErrors As Boolean = False
            Dim OpType As String = ""

            Return True
        Catch ex As Exception
            Debug.WriteLine("ExEx: " & ex.Message.ToString)
            Return False
        End Try
    End Function

    Function DownloadWorkdayFileFromBackFallServerForCombine() As Boolean
        Try
            Dim downrequest As New DownloadRequest
            Dim filerequest As New RemoteFileInfo

            downrequest.FileName = "ArbeitsaufzeichnungenV1.s3db"

            BackFallService.DownloadFile(downrequest.FileName, False, filerequest.FileByteStream)

            Dim buffer As Byte() = New Byte(6499) {}
            Dim bytesRead As Integer = 0

            Dim writestream As New System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tempArbeitsaufzeichnungenV1.s3db"), IO.FileMode.Create)

            bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            While bytesRead > 0
                writestream.Write(buffer, 0, bytesRead)
                writestream.Flush()
                buffer = New Byte(6499) {}
                bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            End While

            writestream.Close()

            Dim HasErrors As Boolean = False
            Dim OpType As String = ""

            Return True
        Catch ex As Exception
            Debug.WriteLine("ExEx: " & ex.Message.ToString)
            Return False
        End Try
    End Function

    Function UploadMandateFileToBackFallServer() As Boolean
        If mandateDb IsNot Nothing Then
            mandateDb.Dispose()
        End If
        Dim uploadRequestInfo As New RemoteFileInfo
        If System.IO.File.Exists(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "mandate.s3db")) Then


            Dim fileinfo As New System.IO.FileInfo(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "mandate.s3db"))
            If fileinfo.Length > 0 Then
                Try
                    Using stream As System.IO.Stream = New System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "mandate.s3db"), IO.FileMode.Open, IO.FileAccess.Read)
                        uploadRequestInfo.FileName = fileinfo.Name
                        uploadRequestInfo.Length = fileinfo.Length
                        uploadRequestInfo.FileByteStream = stream
                        uploadRequestInfo.OnDelete = True
                        BackFallService.UploadFile(uploadRequestInfo.FileName, uploadRequestInfo.Length, uploadRequestInfo.OnDelete, uploadRequestInfo.FileByteStream)
                    End Using

                    Dim HasErrors As Boolean = False
                    Dim OpType As String = ""
                    Dim ErrorDescr As Exception = BackFallService.GetOperationStatus(HasErrors, OpType)
                    If HasErrors = True Then
                        Debug.WriteLine(ErrorDescr.Message.ToString)
                        Return False
                    Else
                        Return True
                    End If

                Catch ex As Exception
                    My.Computer.FileSystem.WriteAllText(files, "Upload Mandate Error: " & Date.Now.ToString & ex.Message.ToString & vbCrLf & ex.Source.ToString & ";" & vbCrLf, True)
                    Return False
                End Try


            Else
                InitMandateDBFile()
                Return True
            End If
        Else
            My.Computer.FileSystem.WriteAllText(files, "Upload Mandate not possible/necessary " & Date.Now.ToString & ";" & vbCrLf, True)
            Return True
        End If
    End Function

    Function DownloadDatabaseFileFromBackFallServer() As Boolean
        Try
            Dim downrequest As New DownloadRequest
            Dim filerequest As New RemoteFileInfo

            downrequest.FileName = "ArbeitsaufzeichnungenV1.s3db"

            BackFallService.DownloadFile(downrequest.FileName, False, filerequest.FileByteStream)

            Dim buffer As Byte() = New Byte(6499) {}
            Dim bytesRead As Integer = 0

            Dim writestream As New System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ArbeitsaufzeichnungenV1.s3db"), IO.FileMode.Create)

            bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            While bytesRead > 0
                writestream.Write(buffer, 0, bytesRead)
                writestream.Flush()
                buffer = New Byte(6499) {}
                bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            End While

            writestream.Close()

            Dim HasErrors As Boolean = False
            Dim OpType As String = ""
            Dim ErrorDescr As Exception = BackFallService.GetOperationStatus(HasErrors, OpType)
            If HasErrors = True Then
                Debug.WriteLine(ErrorDescr.Message.ToString)
                Return False
            Else
                Return True
            End If
            Return True
        Catch ex As Exception
            My.Computer.FileSystem.WriteAllText(files, "DownloadDB failed: Error: " & ex.Message.ToString & vbCrLf & ex.Source.ToString & Date.Now.ToString & ex.Message.ToString & vbCrLf & ex.Source.ToString & ";" & vbCrLf, True)
            Return False
        End Try

    End Function


    Function DownloadGPSBilderFileFromBackFallServer() As Boolean
        Try
            Dim downrequest As New DownloadRequest
            Dim filerequest As New RemoteFileInfo

            downrequest.FileName = GPSBilderDBName

            BackFallService.DownloadFile(downrequest.FileName, False, filerequest.FileByteStream)

            Dim buffer As Byte() = New Byte(6499) {}
            Dim bytesRead As Integer = 0

            Dim writestream As New System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, GPSBilderDBName), IO.FileMode.Create)

            bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            While bytesRead > 0
                writestream.Write(buffer, 0, bytesRead)
                writestream.Flush()
                buffer = New Byte(6499) {}
                bytesRead = filerequest.FileByteStream.Read(buffer, 0, buffer.Length)
            End While

            writestream.Close()

            Dim HasErrors As Boolean = False
            Dim OpType As String = ""
            Dim ErrorDescr As Exception = BackFallService.GetOperationStatus(HasErrors, OpType)
            If HasErrors = True Then
                Debug.WriteLine(ErrorDescr.Message.ToString)
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            My.Computer.FileSystem.WriteAllText(files, "DownloadDB failed: Error: " & ex.Message.ToString & vbCrLf & ex.Source.ToString & Date.Now.ToString & ex.Message.ToString & vbCrLf & ex.Source.ToString & ";" & vbCrLf, True)
            Return False
        End Try
    End Function

    Function UploadDatabaseFileToBackFallServer() As Boolean
        If db IsNot Nothing Then
            db.Dispose()
        End If
        Dim uploadRequestInfo As New RemoteFileInfo
        If System.IO.File.Exists(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ArbeitsaufzeichnungenV1.s3db")) Then
            Dim fileinfo As New System.IO.FileInfo(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ArbeitsaufzeichnungenV1.s3db"))
            If fileinfo.Length > 0 Then
                Try
                    Using stream As System.IO.Stream = New System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ArbeitsaufzeichnungenV1.s3db"), IO.FileMode.Open, IO.FileAccess.Read)
                        uploadRequestInfo.FileName = fileinfo.Name
                        uploadRequestInfo.Length = fileinfo.Length
                        uploadRequestInfo.FileByteStream = stream
                        uploadRequestInfo.OnDelete = True
                        BackFallService.UploadFile(uploadRequestInfo.FileName, uploadRequestInfo.Length, uploadRequestInfo.OnDelete, uploadRequestInfo.FileByteStream)
                    End Using

                    Dim HasErrors As Boolean = False
                    Dim OpType As String = ""
                    Dim ErrorDescr As Exception = BackFallService.GetOperationStatus(HasErrors, OpType)
                    If HasErrors = True Then
                        Debug.WriteLine(ErrorDescr.Message.ToString)
                        Return False
                    Else
                        Return True
                    End If

                Catch ex As Exception
                    My.Computer.FileSystem.WriteAllText(files, "Upload Arbeitsaufzeichnungen Error: " & Date.Now.ToString & ex.Message.ToString & vbCrLf & ex.Source.ToString & ";" & vbCrLf, True)
                    Return False
                End Try


            Else
                InitArbeitsaufzeichnungenDBFile()
                Return True
            End If
        Else
            My.Computer.FileSystem.WriteAllText(files, "Upload Arbeitsaufzeichnungen not possible/necessary " & Date.Now.ToString & ";" & vbCrLf, True)
            Return True

        End If
    End Function

    Dim GPSBilderDBName = "GPSBilderDatenbank.s3db"
    Function UploadGPSBilderDBFileToBackFallServer() As Boolean
        If gpsdb IsNot Nothing Then
            gpsdb.Dispose()
        End If

        Dim uploadRequestInfo As New RemoteFileInfo
        If System.IO.File.Exists(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, GPSBilderDBName)) Then
            Dim fileinfo As New System.IO.FileInfo(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, GPSBilderDBName))
            If fileinfo.Length > 0 Then
                Try
                    Using stream As System.IO.Stream = New System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, GPSBilderDBName), IO.FileMode.Open, IO.FileAccess.Read)
                        uploadRequestInfo.FileName = fileinfo.Name
                        uploadRequestInfo.Length = fileinfo.Length
                        uploadRequestInfo.FileByteStream = stream
                        uploadRequestInfo.OnDelete = True
                        BackFallService.UploadFile(uploadRequestInfo.FileName, uploadRequestInfo.Length, uploadRequestInfo.OnDelete, uploadRequestInfo.FileByteStream)
                    End Using

                    Dim HasErrors As Boolean = False
                    Dim OpType As String = ""
                    Dim ErrorDescr As Exception = BackFallService.GetOperationStatus(HasErrors, OpType)
                    If HasErrors = True Then
                        Debug.WriteLine(ErrorDescr.Message.ToString)
                        Return False
                    Else
                        Return True
                    End If

                Catch ex As Exception
                    My.Computer.FileSystem.WriteAllText(files, "Upload GPSBildDB Error: " & Date.Now.ToString & ex.Message.ToString & vbCrLf & ex.Source.ToString & ";" & vbCrLf, True)
                    Return False
                End Try


            Else
                InitBilderDBFile()
                Return True
            End If
        Else
            My.Computer.FileSystem.WriteAllText(files, "Upload GPSBildDB not possible/necessary " & Date.Now.ToString & ";" & vbCrLf, True)
            Return True
        End If
    End Function

    Public Function CombineCurrentVersionOfMandateWithBackUpVersionMandate() As Boolean
        Dim download = DownloadMandateFileFromBackFallServerForCombine()

        If download Then
            Dim temparbeitsaufzeichnungen As New MandateAufstellung.Main(InitVariousDbFileConnString("tempmandate.s3db"))

            For Each a In temparbeitsaufzeichnungen.MandateAnzahlen
                Dim b = From c In mandateDb.MandateAnzahlen
                        Where c.ID = a.ID
                        Select c

                If b.Count > 0 Then
                    Continue For
                Else
                    mandateDb.MandateAnzahlen.InsertAllOnSubmit(b)
                    mandateDb.SubmitChanges()
                End If
            Next
            mandateDb.Dispose()
            Dim retval = UploadMandateFileToBackFallServer()
            If System.IO.File.Exists(VariousDBPath("tempmandate.s3db")) Then
                System.IO.File.Delete(VariousDBPath("tempmandate.s3db"))
            End If

            Return True

        Else
            Return False
        End If

    End Function

    Public Function CombineCurrentVersionOfArbeitsaufzeichnungenWithBackUpVersionWorkdays() As Boolean
        Dim download = DownloadWorkdayFileFromBackFallServerForCombine()
        If download Then
            Dim temparbeitsaufzeichnungen As New ArbAuf.Main(InitVariousDbFileConnString("tempArbeitsaufzeichnungenV1.s3db"))
            If db Is Nothing Then
                InitArbeitsaufzeichnungenDBFile()
            End If
            For Each a In temparbeitsaufzeichnungen.Arbeitsaufzeichnung
                Dim b = From c In db.Arbeitsaufzeichnung
                        Where c.ID = a.ID
                        Select c

                If b.Count > 0 Then
                    Continue For
                Else
                    db.Arbeitsaufzeichnung.InsertAllOnSubmit(b)

                End If
            Next

            For Each d In temparbeitsaufzeichnungen.Typen
                Dim b = From c In db.Typen
                        Where c.ID = d.ID
                        Select c

                If b.Count > 0 Then
                    Continue For
                Else
                    db.Typen.InsertAllOnSubmit(b)
                End If
            Next
            db.SubmitChanges()
            db.Dispose()
            Dim retval = UploadDatabaseFileToBackFallServer()
            If System.IO.File.Exists(VariousDBPath("tempArbeitsaufzeichnungenV1.s3db")) Then
                System.IO.File.Delete(VariousDBPath("tempArbeitsaufzeichnungenV1.s3db"))
            End If

            Return True
        Else
            Return False
        End If
    End Function
#End Region

#End Region

#Region "searching..."

    Function SumUpSearch(ByVal searchitems As List(Of MandateAnzahlen)) As SumUps
        Dim pg As Integer
        Dim stvo As Integer

        For Each a In searchitems
            pg += a.Pg
            stvo += a.StVo
        Next

        Dim ntype As New SumUps
        With ntype
            .GesamtAnzahl = pg + stvo
            .PgAnzahl = pg
            .StVoAnzahl = stvo
        End With
        Return ntype
    End Function


    Public Function search(ByVal datum As String, ByVal rayon As String, ByVal type As String) As List(Of MandateAnzahlen)

        Dim query As IEnumerable(Of MandateAnzahlen) = listofmandate

        If datum <> "" Then
            query = query.Where(Function(f As MandateAnzahlen)
                                    If f.Datum.Equals(datum) Then
                                        Return True
                                    End If
                                    Return False
                                End Function)
        End If

        If rayon <> "" Then
            query = query.Where(Function(f As MandateAnzahlen)
                                    If f.Rayon.Equals(rayon) Then
                                        Return True
                                    End If
                                    Return False
                                End Function)
        End If

        If type <> "" Then
            query = query.Where(Function(f As MandateAnzahlen)
                                    If f.Typ.Equals(type) Then
                                        Return True
                                    End If
                                    Return False
                                End Function)
        End If

        Dim retlist As New List(Of MandateAnzahlen)

        For Each g In query
            retlist.Add(g)
        Next
        Return retlist
    End Function


#End Region

#Region "inits"
    Public ListOfRayone As New List(Of String)

    Sub InitRayone()
        For a = 1 To 83
            ListOfRayone.Add(a)
        Next
    End Sub
#End Region
End Module


Public Class Mandat
    Property Id As String
    Property Datum As String
    Property StvoAnzahl As Integer
    Property PgAnzahl As Integer
    Property Type As String
    Property Rayon As String
    Property Stornos As String
    Property Anzeigen As String
    Property TB02 As String
    ReadOnly Property Gesamt As Integer
        Get
            Return StvoAnzahl + PgAnzahl
        End Get
    End Property

    Public Overrides Function Equals(obj As Object) As Boolean
        Dim mand = CType(obj, Mandat)

        Return Me.Equls(mand)
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Id.GetHashCode
    End Function

    Private Function Equls(ByVal tocompare As Mandat)
        If tocompare Is Nothing Then Return False
        If String.IsNullOrEmpty(tocompare.Id) Then Return False
        Return Id.Equals(tocompare)
    End Function
End Class

Public Class SumUps
    Property StVoAnzahl As Integer
    Property PgAnzahl As Integer
    Property GesamtAnzahl As Integer
End Class

Public Class FunctionsReturn
    Property hasError As Boolean
    Property Errors As Exception
End Class